package com.qianfeng.xinyihezi.xinyihezi.jsondecode;

import android.util.Log;

import com.qianfeng.xinyihezi.xinyihezi.model.ClassiftLeftBanner;
import com.qianfeng.xinyihezi.xinyihezi.model.ClassiftLeftData;
import com.qianfeng.xinyihezi.xinyihezi.model.LeftData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tcoy on 2016/11/14.
 */

public class DecodeLeftClassift {
    private String TAG = getClass().getSimpleName();
    private List<ClassiftLeftBanner> classiftLeftBannerList;
    private List<ClassiftLeftData> classiftLeftDataList;
    private LeftData leftData;

    public DecodeLeftClassift() {
        classiftLeftBannerList = new ArrayList<>();
        classiftLeftDataList = new ArrayList<>();
        leftData = new LeftData();
    }

    public LeftData startDecode(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArrayData = jsonObject.optJSONArray("data");
            for (int i = 0; i < jsonArrayData.length(); i++) {
                JSONObject jsonObjectAll = jsonArrayData.optJSONObject(i);
                JSONObject jsonObjectBanner = jsonObjectAll.optJSONObject("banner");
                ClassiftLeftBanner classiftLeftBanner = new ClassiftLeftBanner(
                        jsonObjectBanner.optString("image_url"),
                        jsonObjectBanner.optString("name"),
                        jsonObjectBanner.optString("table_id"),
                        jsonObjectBanner.optString("table_name"),
                        jsonObjectBanner.optString("url_name"));
                ClassiftLeftData classiftLeftData = new ClassiftLeftData(classiftLeftBanner, jsonObjectAll.optString("cat_id"), jsonObjectAll.optString("image_url"), jsonObjectAll.optString("name"));
                classiftLeftDataList.add(classiftLeftData);
                classiftLeftBannerList.add(classiftLeftBanner);
            }
            leftData.setClassiftLeftBannerList(classiftLeftBannerList);
            leftData.setClassiftLeftDataList(classiftLeftDataList);
            return leftData;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
